/**
 * 
 */
package org.adempiere.apps.graph;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import layout.TableLayout;

import org.compiere.model.MBSCCardLine;
import org.compiere.model.MBSCPerspective;
import org.compiere.util.Env;

/**
 * @author Ибраев Е.А.
 *
 */
public class BSCCardLinePerspectivePanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8152684396889389882L;

	
	private MBSCCardLine[] cardLines = null;
	private int BSC_Perspective_ID = 0;
	private MBSCPerspective perspective;
	private BSCCardLinePanel[] cardLinePanels = null;
	private TableLayout tableLayout = null;
	private double[][] tableSize;	
	private Dimension panelDimension = null;
	protected int num = 0;
	
	
	public BSCCardLinePerspectivePanel(int _BSC_Perspective_ID, MBSCCardLine[] cl, int initNum) {
		super();
		num = initNum;
		setBorder(BorderFactory.createLineBorder(Color.BLACK));
		setCardLines(cl);
		setBSC_Perspective_ID(_BSC_Perspective_ID);
		initLayout();
		initView();
	}
	
	protected void initLayout() {
		tableLayout = new TableLayout(initTableSize());
		setLayout(tableLayout);
	}
	
	protected double[][] initTableSize() {
		int Size = 1 + (cardLines != null ? cardLines.length : 0);
		Size = (Size < BSCTableHeaderPanel.tableHeader.length ? BSCTableHeaderPanel.tableHeader.length: Size);
		tableSize = new double[2][Size];
		for(int i = 0; i < Size; i++) {
			tableSize[0][i] = (i < Size ? getCellWidth(i) : 0);
			tableSize[1][i] = ( i < 1 ? BSCTableHeaderPanel.cellHeight[0] : getCellHeight(i));
		}		
		return tableSize;
	}
	
	protected int getCellHeight(int i) {
		int result = 0;
		if (cardLines != null && i > 0 && i - 1 < cardLines.length) {
			result = BSCCardLinePanel.cellHeight[0] ;
		}
		return result;
	}
	
	protected int getCellWidth(int i) {
		int result = 0;
		if (i == 0) {
			for(int j:BSCTableHeaderPanel.cellWidth) {
				result += j;
			}
		}
		return result;
	}
		public void initView() {
		removeAll();
		panelDimension = initDimesion();
		setSize(panelDimension);
		setPreferredSize(panelDimension);
		setMaximumSize(panelDimension);
		setMinimumSize(panelDimension);
		
		if (perspective != null && cardLines != null && cardLines.length > 0) {
			initCardLines();
			add(initTitlePanel(),"0,0");
			if (cardLinePanels != null) {
				int i = 1;
				for(BSCCardLinePanel panel:cardLinePanels) {
					String pos = "0,"+Integer.toString(i++);
					add(panel,pos);
				}
			}
		}
	}
	
	private Dimension initDimesion() {
		int width = 0;
		int height = 0;
			
		for(int i=0; i < tableSize[0].length; i++) {
			width += tableSize[0][i];
			height += tableSize[1][i];
		}
			
		panelDimension = new Dimension(width+1,height+1);
		return panelDimension;
	}
		
	public JPanel initTitlePanel() {
		JPanel titlePanel = new JPanel();
		JLabel titleLabel = new JLabel((perspective != null ? perspective.getName() : ""));
		titlePanel.add(titleLabel);
		titlePanel.setBorder(getBorder());
		return titlePanel;
	}
	
	public void initCardLines() {
		if(cardLines != null && cardLines.length > 0) {
			cardLinePanels = new BSCCardLinePanel[cardLines.length];
			for(int i=0; i < cardLines.length; i++) {
				cardLinePanels[i] = new BSCCardLinePanel(cardLines[i], num);
				num = cardLinePanels[i].getNum();
				cardLinePanels[i].setBorder(getBorder());
			}
		}
	}
	
	public MBSCCardLine[] getCardLines() {
		return cardLines;
	}

	public void setCardLines(MBSCCardLine[] cardLines) {
		this.cardLines = cardLines;
	}

	public int getBSC_Perspective_ID() {
		return BSC_Perspective_ID;
	}

	public void setBSC_Perspective_ID(int bSC_Perspective_ID) {
		if (bSC_Perspective_ID > 0) {
			perspective = new MBSCPerspective(Env.getCtx(),bSC_Perspective_ID,null);
		}
		BSC_Perspective_ID = bSC_Perspective_ID;
	}
	
	public int getNum() {
		return num;
	}
}
